﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace FootBallMatchProject.Piography.Admin
{
    public partial class CountryList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RefreshList(0);
            }
        }
        private void RefreshList(int fromsearch)
        {
            DataClassLayer.Country country = new DataClassLayer.Country();
            DataClassLayer.CountryCollection countrys= new DataClassLayer.CountryCollection();
            if (fromsearch == 1)
            {
                if (cmbSearch.SelectedIndex != 0 &&
                    cmbSearch.SelectedIndex != -1)
                {
                    if (cmbSearch.SelectedValue == "1")
                    {
                        country.Name = txtSearch.Text;
                    }
                    DataTable table = countrys.FillList(country);
                    if (table.Rows.Count > 0)
                    {
                        gvView.DataSource = table;
                        gvView.DataBind();
                    }
                }
            }
            else
            {
                DataTable table = countrys.FillList(country);
                if (table.Rows.Count > 0)
                {
                    gvView.DataSource = table;
                    gvView.DataBind();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            RefreshList(1);
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddEditCountry.aspx?operation=Add&id=0");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            CheckBox ch = new CheckBox();
            LinkButton lb = new LinkButton();
            DataClassLayer.Country country = new DataClassLayer.Country();
            DataClassLayer.CountryCollection countrys = new DataClassLayer.CountryCollection();
            for (int i = 0; i < gvView.Rows.Count; i++)
            {
                ch = ((CheckBox)gvView.Rows[i].FindControl("cbDetail"));
                if (ch.Checked)
                {
                    lb = ((LinkButton)gvView.Rows[i].FindControl("LinkButton1"));
                    country.ID = int.Parse(lb.CommandArgument);
                    countrys.Delete(country);
                    RefreshList(0);
                }
            }
        }

        protected void cbDetail_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ch = new CheckBox();
            ch = ((CheckBox)gvView.HeaderRow.FindControl("cbHeader"));
            ch.Checked = DataAccessObject.StaticClass.CheckAll(gvView);
        }

        protected void cbHeader_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = new CheckBox();
            if (DataAccessObject.StaticClass.CheckHeader(gvView))
            {
                for (int i = 0; i < gvView.Rows.Count; i++)
                {
                    cb = ((CheckBox)gvView.Rows[i].FindControl("cbDetail"));
                    cb.Checked = true;
                }
            }
            else
            {
                for (int i = 0; i < gvView.Rows.Count; i++)
                {
                    cb = ((CheckBox)gvView.Rows[i].FindControl("cbDetail"));
                    cb.Checked = false;
                }
            }
        }

        protected void gvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvView.PageIndex = e.NewPageIndex;
            RefreshList(0);
        }

        protected void gvView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Response.Redirect("AddEditCountry.aspx?operation=Edit&id=" + e.CommandArgument);
        }

    }
}